const Koa = require('koa');
const app = new Koa();
const server = require('http').Server(app.callback());
const io = require('socket.io')(server);
const port = 3000;

server.listen(process.env.PORT || port, () => {
    console.log(`app run at : http://127.0.0.1:${port}`);
})

let soketArr = []
io.of('/chat').on('connection', (socket) => {
    console.log('初始化成功！',socket.id);
    soketArr.push(socket.id) //连接者id


    socket.on('broadcast', (data)=> {
        socket.emit('broadcast', data);
        socket.emit('broadcast', socket.id);
    })
    // 私人消息
    socket.on('chat', (id,data)=> {
        let socketID = id
        console.log('socketID！',id);
        socket.to(socketID).emit('chat', data)
    })
    setTimeout( () => {
        socket.emit('broadcast', '我是初始化3s后的返回消息')
        socket.to(socket.id).emit('chat', '你的id ' + socket.id)
    }, 3000)


})

